var mysql = require('mysql2/promise');
var pool = mysql.createPool({
    host: 'localhost',
    port: 3306,
    user: 'root',
    password: 'root',
    database: 'zzj1',
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0,
    supportBigNumbers: true
});
module.exports.bookList = async function () {
    var con = await pool.getConnection();
    var sql = 'select * from bookinfo';
    var data = await con.execute(sql);
    con.release();
    return data[0]
}
module.exports.bookId = async function (id) {
    var con = await pool.getConnection();
    var sql = 'select * from bookinfo where id=?';
    var data = await con.execute(sql, [id]);
    con.release();
    return data[0]
}
module.exports.bookAdd = async function (bookData) {
    try {
        var con = await pool.getConnection();
        var sql = 'insert into bookinfo (`isbn`,`name`,`author`,`press`,`price`,`pubdate`) values (?,?,?,?,?,?)';
        var data = await con.execute(sql, [bookData.isbn, bookData.name, bookData.author, bookData.press, bookData.price, new Date()]);
        con.release();
        return Promise.resolve(data[0])
    } catch (err) {
        return Promise.reject(err)
    }

}
module.exports.bookDel = async function (id) {
    var con = await pool.getConnection();
    var sql = 'delete from bookinfo where id=?';
    var data = await con.execute(sql, [id]);
    con.release();
    return data[0]
}
module.exports.bookUpt = async function (bookData) {
    try {
        var con = await pool.getConnection();
        var sql = 'update bookinfo set `isbn`=?,`name`=?,`author`=?,`press`=?,`price`=? where id=?';
        var data = await con.execute(sql, [bookData.isbn, bookData.name, bookData.author, bookData.press, bookData.price, bookData.id]);
        con.release();
        return data[0]
    } catch (err) {
        return Promise.reject(err)
    }
}